package com.example.myapplication.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.mroom.database.StudentRoomDao;
import com.example.myapplication.mroom.database.UserRoomDao;
import com.example.myapplication.mroom.database.UserRoomDb;
import com.example.myapplication.mroom.pojo.UserRoom;
import com.example.myapplication.ui.fragments.GreenDaoFragment;
import com.example.myapplication.ui.fragments.OrmLiteFragment;
import com.example.myapplication.ui.fragments.RoomFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.test.greendao.StudentGreenDao;
import com.test.greendao.UserGreenDao;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

public class MainActivity extends AppCompatActivity {

    private UserGreenDao userGreenDao;
    private StudentGreenDao studentGreenDao;
    private StudentRoomDao studentRoomDao;

    private ActivityMainBinding viewBinding;
    private DataAdapters dataAdapters;
    private ExecutorService executorService = Executors.newFixedThreadPool(5);;

    //Room
    private UserRoomDao userRoomDao;

    /**
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());

        dataAdapters = new DataAdapters(new ArrayList<>());

        List<Fragment> fragments = new ArrayList<>();
        String[] tabs = new String[]{"OrmLite","GreenDao","Room"};
        viewBinding.viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        fragments.add(new OrmLiteFragment());
        fragments.add(new GreenDaoFragment());
        fragments.add(new RoomFragment());

        viewBinding.viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });
        viewBinding.viewPager.setOffscreenPageLimit(3);

        new TabLayoutMediator(viewBinding.nav, viewBinding.viewPager, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabs[position]);
            }
        }).attach();

//        initView();

        //room
//        initRoomDao();
        viewBinding.go.setOnClickListener(v -> {
            startActivity(new Intent(this, MainActivity2.class));
        });

//        //插入
//        viewBinding.g1.setOnClickListener(v -> {
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    List<StudentGreen> userGreenList = new ArrayList<>();
//                    for (int i = 0; i < 10000; i++) {
//                        StudentGreen userGreen = new StudentGreen();
//                        userGreen.setName("name" + i);
//                        userGreenList.add(userGreen);
//                    }
//                    long startTime = System.currentTimeMillis();
//                    studentGreenDao.insertInTx(userGreenList);
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==插入studentGreen表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    List<UserGreen> userGreenList = new ArrayList<>();
//                    for (int i = 0; i < 10000; i++) {
//                        UserGreen userGreen = new UserGreen();
//                        userGreen.setName("name" + i);
//                        userGreen.setMoney(111);
//                        userGreenList.add(userGreen);
//                    }
//                    long startTime = System.currentTimeMillis();
//                    userGreenDao.insertInTx(userGreenList);
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==插入userGreen表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//        });
//
//        //查询
//        viewBinding.g2.setOnClickListener(v ->{
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    long startTime = System.currentTimeMillis();
//                    List<StudentGreen> studentGreens = studentGreenDao.loadAll();
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==查询studentGreen表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    long startTime = System.currentTimeMillis();
//                    userGreenDao.loadAll();
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==查询userGreen表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//        });

        //Room插入


    }



//    private void deleteView() {
//        viewBinding.ls.removeAllViews();
//    }

    private void initRoomDao() {

        UserRoomDb userRoomDb = UserRoomDb.get(this);
        userRoomDao = userRoomDb.userRoomDao();
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                UserRoom name222220 = new UserRoom(1, "name");
                name222220.setId(15);
                long startTime = System.currentTimeMillis();
                userRoomDao.queryAll();
//                userRoomDao.upData(name222220);
//                userRoomDao.delete(name222220);
                Log.e("kotlin", "Room查询所有---->" + (System.currentTimeMillis() - startTime) + "ms");
            }
        });


//        插入数据

//        viewBinding.r1.setOnClickListener(v -> {
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    List<UserRoom> userBeans = new ArrayList<>();
//                    for (int i = 0; i < 10000; i++) {
//                        UserRoom dasda = new UserRoom(0, "name" + i);
//                        dasda.setAge(222);
//                        userBeans.add(dasda);
//                    }
//                    long startTime = System.currentTimeMillis();
//                    userRoomDao.insert(userBeans);
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==插入user_room表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });

//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    List<StudentRoom> list = new ArrayList<>();
//                    for (int i = 0; i < 10000; i++) {
//                        StudentRoom studentRoom = new StudentRoom();
//                        studentRoom.setName("name" + i);
//                        list.add(studentRoom);
//                    }
//                    long startTime = System.currentTimeMillis();
//                    studentRoomDao.insertToStu(list);
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==插入student_room表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//        });

//        viewBinding.r2.setOnClickListener(v -> {
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    long startTime = System.currentTimeMillis();
//                    userRoomDao.queryAll();
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==查询user_room表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//
//            executorService.execute(new Runnable() {
//                @Override
//                public void run() {
//                    long startTime = System.currentTimeMillis();
//                    studentRoomDao.queryFromStu();
//                    long endTime = System.currentTimeMillis() - startTime;
//                    long id = Thread.currentThread().getId();
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            addView("线程：" + id + "==查询student_room表耗时->" + endTime + "ms");
//                        }
//                    });
//                }
//            });
//        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ExecutorsUtils.get().getFixThread() != null) {
            ExecutorsUtils.get().getFixThread().shutdown();
        }
    }




}